package com.thinkgem.jeesite.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.SysArea;
import com.thinkgem.jeesite.modules.sys.entity.SysOffice;
import com.thinkgem.jeesite.modules.sys.entity.SysUser;
import com.thinkgem.jeesite.modules.sys.service.SysAreaService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 区域Controller
 *
 * @author kongxiangsheng
 * @version 2013-5-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysArea")
public class SysAreaController extends BaseController {
    @Autowired
    private SysAreaService sysAreaService;

    @RequiresPermissions("sys:sysArea:view")
    @RequestMapping(value = {"list", ""})
    public String list(SysArea sysArea, Model model) {
        model.addAttribute("list", sysAreaService.getAllOrdered());
        return "modules/sys/SysArea/list";
    }

    @RequiresPermissions("sys:sysArea:view")
    @RequestMapping(value = "form")
    public String form(SysArea sysArea, Model model) {
        if (StringUtils.isNotBlank(sysArea.getId())) {
            sysArea = sysAreaService.getById(sysArea.getId());
        }
        if (StringUtils.isBlank(sysArea.getParentId())) {
            SysUser user = UserUtils.getUser();
            SysOffice office = UserUtils.getOffice(user.getOfficeId());
            sysArea.setParentId(office.getAreaId());
        }
        model.addAttribute("sysArea", sysArea);
        return "modules/sys/SysArea/form";
    }

    @RequiresPermissions("sys:sysArea:edit")
    @RequestMapping(value = "save")
    public String save(SysArea sysArea, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, sysArea)) {
            return form(sysArea, model);
        }
        sysAreaService.saveArea(sysArea);
        addMessage(redirectAttributes, "保存区域'" + sysArea.getName() + "'成功");
        return "redirect:" + adminPath + "/sys/sysArea/";
    }

    @RequiresPermissions("sys:sysArea:edit")
    @RequestMapping(value = "delete")
    public String delete(SysArea sysArea, RedirectAttributes redirectAttributes) {
        sysAreaService.removeById(sysArea);
        addMessage(redirectAttributes, "删除区域成功");
        return "redirect:" + adminPath + "/sys/sysArea/";
    }

    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeValue")
    public String treeValue(@RequestParam(required = true) String id, HttpServletResponse response) {
        String areaName = "";
        SysArea sysArea = sysAreaService.getById(id);
        if (null != sysArea) {
            areaName = sysArea.getName();
        }
        return areaName;
    }

    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<SysArea> list = sysAreaService.getAllOrdered();
        for (int i = 0; i < list.size(); i++) {
            SysArea e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }
}
